﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OnlineStore.Data_Access_Layer;
using System.Data.Objects;
using System.Windows.Forms;

namespace OnlineStore.Business_Logic_Layer
{
    class BizOrder
    {

        private OnlineStoreEntities eOnlineStoreEntities;
        public BizOrder()
        {
            eOnlineStoreEntities = new OnlineStoreEntities();
        }
        public void GetMaxOrderID()
        {
            
            throw new System.NotImplementedException();
        }
        // Get Order by Order_ID => Get Order by key word
        public List<tbOrder> GetOrderbyKeyWord(string strKey, string strValue)
        {
            List<tbOrder> OrderList = new List<tbOrder>();
            try
            {               
                if (strKey == "")
                    OrderList = eOnlineStoreEntities.tbOrders.ToList();

                if (strKey == "ID")
                {
                    int nID = int.Parse(strValue);
                    OrderList = eOnlineStoreEntities.tbOrders.Where(i => i.Order_Id == nID).ToList();
                }
                if (strKey == "Status")
                    OrderList = eOnlineStoreEntities.tbOrders.Where(i => i.Status == strValue).ToList();
                if (strKey == "Customer Name")
                {   
                    OrderList = (from Order in eOnlineStoreEntities.tbOrders
                                  join Cus in eOnlineStoreEntities.tbCustomers
                                      on Order.Customer_ID equals Cus.Cus_ID
                                  where Cus.Cus_Name.Contains(strValue)
                                  select Order).ToList();
                }
                if (strKey == "Phone")
                {
                    OrderList = (from Order in eOnlineStoreEntities.tbOrders
                                 join Cus in eOnlineStoreEntities.tbCustomers
                                     on Order.Customer_ID equals Cus.Cus_ID
                                 where Cus.Phone.Contains(strValue)
                                 select Order).ToList();
                }
                if (strKey == "Order Date")
                {
                }                
            }
            catch (Exception ex)
            {
                ex.ToString();                
            }
            return OrderList;
        }
        // Get order detail by order_id
        public List<tbOrderDetail> GetOrderDetailbyOrderID(int nOrderID)
        {
            List<tbOrderDetail> eOrderDetailList = new List<tbOrderDetail>();
            eOrderDetailList = eOnlineStoreEntities.tbOrderDetails.Where(i => i.Order_ID == nOrderID).ToList();
            return eOrderDetailList;
        }
        // Update status of order
        public void UpdateOrderStatus(string strNewStatus, int nOrderID)
        {
            tbOrder eOrder = new tbOrder();
            eOrder = GetOrderbyKeyWord("ID", nOrderID.ToString()).First();
            eOrder.Status = strNewStatus;
            eOnlineStoreEntities.SaveChanges();
        }
        public bool ValidateData(DataGridViewRowCollection OrderDetail)
        {
            // check confirm qty on datagrid
            try
            {
                for (int i = 0; i < OrderDetail.Count; i++)
                    if (OrderDetail[i].Cells[5].Value != null)
                        int.Parse(OrderDetail[i].Cells[5].Value.ToString());
                return true;
            }
            catch (Exception ex)
            {
                ex.ToString();
                return false;
            }
        }
        public int UpdateOrderDetail(int nOrderID, DataGridViewRowCollection OrderDetail)
        {
            try
            {
                BizProduct eBizProduct = new BizProduct();
                int ni, nProductID;
                for (ni = 0; ni < OrderDetail.Count; ni++)
                {
                    // update OrderQty in Product item.
                    eBizProduct.UpdateConfirmOrderProduct(int.Parse(OrderDetail[ni].Cells[0].Value.ToString()), int.Parse(OrderDetail[ni].Cells[4].Value.ToString()), int.Parse(OrderDetail[ni].Cells[5].Value.ToString()));

                    // update confirm qty on order detail.
                    tbOrderDetail eOrderDetail = new tbOrderDetail();
                    nProductID = int.Parse(OrderDetail[ni].Cells[0].Value.ToString());
                    eOrderDetail = eOnlineStoreEntities.tbOrderDetails.First (i => i.Order_ID == nOrderID && i.Product_ID == nProductID);
                    eOrderDetail.Confirm_Qty = int.Parse(OrderDetail[ni].Cells[5].Value.ToString());
                    eOnlineStoreEntities.SaveChanges();
                }
                return ni;
            }
            catch (Exception ex)
            {
                ex.ToString();
                return -1;
            }

        }
        public tbCustomer GetCustomerbyID(int nCusID)
        {
            tbCustomer eCustomer = eOnlineStoreEntities.tbCustomers.First(i => i.Cus_ID == nCusID);
            return eCustomer;
        }        
    }
}
